# -*- coding: utf-8 -*-

'''
统计胎压数据
'''

from datetime import datetime, timedelta
import pandas as pd
from gov.data_process.data import DataProcess
from pao.data import string_to_date, process_db


class TP_Sim_Data(DataProcess):

    def cal_TP_data(self, starttime, endtime, TPID):
        start_date = string_to_date(starttime)
        end_date = string_to_date(endtime)
        res_list = ''

        def process_func(db):
            nonlocal res_list
            collection_tp = db['TP_Monitoring']
            cur = collection_tp.find(
                {'TPID': TPID, 'date': {'$gte': start_date, '$lt': end_date}})
            res_list = list(cur[:])
        process_db(self.db_addr, self.db_port, 'GovNetThing', process_func)
        res_df = pd.DataFrame(res_list)
        res_df = res_df[['date', 'tirePos', 'pressure', 'temperature']]
        res = res_df.to_json(orient='index', force_ascii=False)
        return res
